﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace sqlDALgen.Renderers
{
    public class ScriptRenderer : IRenderer
    {
        private readonly XmlNode _node;
        private String _name;

        public static IRenderer Get(XmlNode node)
        {
            return new ScriptRenderer(node);
        }

        public ScriptRenderer(XmlNode node)
        {
            _node = node;
        }

        public string Render(TemplateData data)
        {
            var pathNode = _node.SelectSingleNode("path");

            if (pathNode != null)
            {
                var reader = new XmlDocument();
                reader.Load(Path.Combine(Environment.CurrentDirectory
                    , String.Format("{0}.xml", TemplateRenderer.FillData(pathNode.InnerText, data))));

                var templatesNode = _node.SelectSingleNode("templates");

                if (templatesNode != null)
                {
                    if (data.ContainsKey("templates"))
                        data["templates"] = new TemplateData();
                    else
                        data.Add("templates", new TemplateData());

                    foreach (XmlNode template in templatesNode.ChildNodes)
                    {
                        data["templates"].Add(template.Attributes["name"].Value, TemplateData.ValueNode(template.InnerXml));
                    }
                }

                return TemplateRenderer.Get(reader.FirstChild).Render(data);
            }

            var valueNode = _node.SelectSingleNode("value");

            if (valueNode != null)
            {
                var reader = new XmlDocument();
                reader.LoadXml(data.ResolvePath(valueNode.InnerText.Substring(1, valueNode.InnerText.Length - 2)).ToString());

                var templatesNode = _node.SelectSingleNode("templates");

                if (templatesNode != null)
                {
                    if (data.ContainsKey("templates"))
                        data["templates"] = new TemplateData();
                    else
                        data.Add("templates", new TemplateData());

                    foreach (XmlNode template in templatesNode.ChildNodes)
                    {
                        data["templates"].Add(template.Attributes["name"].Value, TemplateData.ValueNode(template.InnerXml));
                    }
                }

                return TemplateRenderer.Get(reader.FirstChild).Render(data);
            }

            _name = TemplateRenderer.FillData(_node.SelectSingleNode("/script/name").InnerText, data);

            var builder = new StringBuilder();
            var nodes = _node.SelectSingleNode("/script/gen").ChildNodes;

            foreach (XmlNode node in nodes)
            {
                builder.Append(TemplateRenderer.Get(node).Render(data));
            }

            return builder.ToString();
        }

        public string FileName
        {
            get { return _name; }
        }
    }
}
